<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\helper;

use builder\tree\HelperInterface;
use builder\tree\util\OperationItemHelper;
use builder\base\InvalidInstanceException;

/**
 * 树组件助手
 * @author cleverstone
 * @since ym2.0
 */
class Tree
{
    /**
     * @var HelperInterface
     */
    private static $_helper;

    /**
     * 树组件操作项组手实例
     * @return OperationItemHelper
     * @throws InvalidInstanceException
     */
    public static function operationItem()
    {
        return self::get(OperationItemHelper::class);
    }

    /**
     * @param string $class 助手类名
     * @return HelperInterface
     * @throws InvalidInstanceException
     */
    public static function get($class)
    {
        if (!empty(self::$_helper[$class])) {
            return self::$_helper[$class];
        }

        $object = new $class();
        if (!$object instanceof HelperInterface) {
            throw new InvalidInstanceException($class . ' must be instance of ' . HelperInterface::class);
        }

        self::$_helper[$class] = $object;
        return $object;
    }
}